package org.study.javafx.study01.alert.xml.controller;

import cn.hutool.core.date.DateUtil;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.net.URL;
import java.util.*;

/**
 * 主界面控制器
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年08月18日
 */
@Slf4j
public class AlertController implements Initializable {
    // 主容器
    public Pane rootPane;

    public Button noneAlertBtn;
    public Button informationAlertBtn;
    public Button warningAlertBtn;
    public Button confirmationAlertBtn;
    public Button errorAlertBtn;
    public Button customAlertBtn;
    public Button fileChooseBtn;
    public TextArea fileTextArea;
    public ImageView imageView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        log.info("initialize：{}", location.getPath());
    }

    /**
     * 弹出框按钮单击事件
     */
    public void onNoneAlertBtnClick(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.NONE);
        // alert.setAlertType(Alert.AlertType.NONE);
        alert.setContentText(DateUtil.now());
        // 增加一个按钮：否则什么操作都不能做
        // alert.getButtonTypes().add(ButtonType.CLOSE);
        alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO, ButtonType.CANCEL);
        alert.show();
    }

    public void onInformationAlertBtnClick(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setContentText(DateUtil.now());
        alert.show();
    }

    public void onWarningAlertBtnClick(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setContentText(DateUtil.now());
        alert.show();
    }

    public void onConfirmationAlertBtnClick(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setContentText(DateUtil.now());
        alert.show();
    }

    public void onErrorAlertBtnClick(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setContentText(DateUtil.now());
        alert.show();
    }

    public void onCustomAlertBtnClick(ActionEvent actionEvent) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        String datetime = DateUtil.now();
        // Alert alert = new Alert(Alert.AlertType.ERROR, "内容信息", ButtonType.OK, ButtonType.CLOSE);
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("这是Title");
        // 自定义图标
        // alert.setGraphic(new ImageView(new ClassPathResource("icon/icon.png").getPath()));
        alert.setHeaderText("这是HeaderText");
        alert.setContentText("这是ContentText\n" +
                "javaVersion：" + javaVersion + "\n" +
                "javafxVersion：" + javafxVersion + "\n" +
                "datetime：" + datetime);
        // 添加按钮
        alert.getButtonTypes().add(ButtonType.CANCEL);
        alert.getButtonTypes().add(ButtonType.CLOSE);
        ButtonType otherButtonType = new ButtonType("我的按钮");
        ButtonType applyButtonType = new ButtonType("应用", ButtonBar.ButtonData.APPLY);
        // 注意setAll()、addAll()区别
        // alert.getButtonTypes().setAll(otherButtonType, applyButtonType);
        alert.getButtonTypes().addAll(otherButtonType, applyButtonType);
        Optional<ButtonType> buttonTypeOptional = alert.showAndWait();
        if (buttonTypeOptional.isPresent()) {
            ButtonType buttonType = buttonTypeOptional.get();
            if (buttonType == ButtonType.APPLY) {
                System.out.println(" ButtonType.APPLY ");
            } else if (buttonType == ButtonType.OK) {
                System.out.println(" ButtonType.OK ");
            } else if (buttonType == ButtonType.CANCEL) {
                System.out.println(" ButtonType.CANCEL ");
            } else if (buttonType == ButtonType.CLOSE) {
                System.out.println(" ButtonType.CLOSE ");
            } else if (buttonType == ButtonType.YES) {
                System.out.println(" ButtonType.YES ");
            } else if (buttonType == ButtonType.NO) {
                System.out.println(" ButtonType.NO ");
            } else if (buttonType == ButtonType.FINISH) {
                System.out.println(" ButtonType.FINISH ");
            } else if (buttonType == ButtonType.NEXT) {
                System.out.println(" ButtonType.NEXT ");
            } else if (buttonType == ButtonType.PREVIOUS) {
                System.out.println(" ButtonType.PREVIOUS ");
            } else if (buttonType == otherButtonType) {
                System.out.println(" ButtonType 自定义按钮-我的按钮 ");
            } else {
                System.out.println(" ButtonType 自定义按钮-其他");
            }
        }
    }

    /**
     * 选择文件按钮单机事件
     */
    public void onFileChooseBtnClick(ActionEvent actionEvent) {
        Window window = rootPane.getScene().getWindow();
        FileChooser fileChooser = new FileChooser();
        // 文件类型过滤器
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("所有类型", "*.*"),
                new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.jpeg", "*.png", "*.gif"),
                new FileChooser.ExtensionFilter("文本文件", "*.pdf", "*.md", "*.txt"),
                new FileChooser.ExtensionFilter("Word文档", "*.ppt", "*.pptx", "*.xls", "*.xlsx", "*.doc", "*.docx")
        );
        // ...
        File file = fileChooser.showOpenDialog(window);
        fileTextArea.setText("");
        if (file != null) {
            // 设置文本区域自动换行
            fileTextArea.setWrapText(true);
            String absolutePath = file.getAbsolutePath();
            fileTextArea.appendText("文件名称：" + file.getName());
            fileTextArea.appendText("\n");
            fileTextArea.appendText("文件路径：" + absolutePath);
            if (this.isImageFile(file)) {
                imageView.setImage(new Image("file:" + absolutePath));
            }
        }
    }

    /**
     * 根据文件后缀判断是否为图片
     */
    private boolean isImageFile(File file) {
        if (file == null || !file.exists()) {
            return false;
        }
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex <= 0 || dotIndex == fileName.length() - 1) {
            // 没有后缀名或者点在文件名末尾，都不是图片
            return false;
        }
        String extension = fileName.substring(dotIndex + 1).toLowerCase();
        return Arrays.asList("jpg", "jpeg", "png", "gif").contains(extension);
    }
}
